package com.github.ginirohikocha.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.ginirohikocha.bean.Database;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class DataServlet
 */
@WebServlet("/DataServlet")
public class DataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DataServlet() {
        super();
    }

    public static SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dfTime = new SimpleDateFormat("HH:mm:ss");
	public static SimpleDateFormat dfHour = new SimpleDateFormat("HH");
	public static SimpleDateFormat dfMin = new SimpleDateFormat("mm");
	public static SimpleDateFormat dfSec = new SimpleDateFormat("ss");
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		Database db = new Database();
		Connection dbCon = db.getDbCon();
		Statement stmt;
		if (request.getParameter("type").equals("hour")) {
			try {
				stmt = dbCon.createStatement();
				int i = Integer.parseInt(dfHour.format(new Date()))-1;
				ResultSet rs = stmt.executeQuery("SELECT * FROM temperature WHERE date='"+dfDate.format(new Date())+"' AND TIME(time) BETWEEN '"+(i<10?"0"+i:i)+":"+dfMin.format(new Date())+":"+dfSec.format(new Date())+"' AND CURTIME()");

				JSONArray dateArr = new JSONArray();
				JSONArray tempArr = new JSONArray();
				
				while(rs.next()) {
					dateArr.add(rs.getString(1)+" "+rs.getString(2));
					tempArr.add(rs.getShort(3));
				}
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("date", dateArr);
				jsonObj.put("temp", tempArr);
				
				response.getWriter().write(jsonObj.toString());
				stmt.close();
				dbCon.close();
				db.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (request.getParameter("type").equals("dynaChart1")) {
			try {
				stmt = dbCon.createStatement();
				int i = Integer.parseInt(dfMin.format(new Date()))-5;
				ResultSet rs = stmt.executeQuery("SELECT * FROM temperature WHERE date='"+dfDate.format(new Date())+"' AND TIME(time) BETWEEN '"+dfHour.format(new Date())+":"+(i>=0?i:0)+":"+dfSec.format(new Date())+"' AND CURTIME()");

				JSONArray dateArr = new JSONArray();
				JSONArray tempArr = new JSONArray();
				
				while(rs.next()) {
					dateArr.add(rs.getString(1)+" "+rs.getString(2));
					tempArr.add(rs.getShort(3));
				}
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("date", dateArr);
				jsonObj.put("temp", tempArr);
				
				response.getWriter().write(jsonObj.toString());
				stmt.close();
				dbCon.close();
				db.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (request.getParameter("type").equals("dynaChart2")) {
			try {
				stmt = dbCon.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT * FROM temperature WHERE DATE(date)=CURDATE() ORDER BY TIME DESC LIMIT 1");

				rs.next();
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("value", rs.getShort(3));
				
				response.getWriter().write(jsonObj.toString());
				stmt.close();
				dbCon.close();
				db.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
